package com.clinic.action.drugbrand;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.clinic.action.common.AuthenticationAction;
import com.clinic.entity.gui.GuiDrugBrand;
import com.clinic.entity.gui.GuiPagination;
import com.clinic.entity.param.Index;
import com.clinic.entity.type.SortProperty;
import com.clinic.logic.drug.DrugBrandLogic;
import com.clinic.util.CommonUtil;

public class DrugBrandAction extends AuthenticationAction {

	@Autowired
	private DrugBrandLogic drugBrandLogic;
	
	private static final long serialVersionUID = 7362281059770740291L;
	private GuiDrugBrand guiDrugBrand;
	private GuiPagination guiPagination;
	private long id;
	private String term;
	private SortProperty sort;
	private long size;
	private int page;
	
	private List<GuiDrugBrand> guiDrugBrands;
	
	public String registration() {
		return ActionResult.SUCCESS;
	}
	
	public String show(){
		guiDrugBrand = drugBrandLogic.getRecordsById(id);
		return ActionResult.SUCCESS;
	}

	public String edit(){
		if(isDoctorLogin() && getLoginUser().isAdmin() == true) {
			guiDrugBrand = drugBrandLogic.getRecordsById(id);
			return ActionResult.SUCCESS;			
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String insert(){
		if(isDoctorLogin() || isAssistantLogin()) {			
			boolean isValidDrugBrand = drugBrandLogic.isValidDrugBrandInsert(guiDrugBrand);
			if(!isValidDrugBrand) {
				addActionError("Failed to insert.This drug brand is available");
			}
			
			if(hasActionErrors()) {
				return ActionResult.ERROR;
			} else {
				guiDrugBrand = drugBrandLogic.insertDrugBrand(guiDrugBrand);
			}
			return ActionResult.SUCCESS;					
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String delete(){
		return ActionResult.SUCCESS;
	}
	
	public String update(){
		if(isDoctorLogin() || isAssistantLogin()) {
			boolean isValidDrugBrandUpdate = drugBrandLogic.isValidDrugBrandUpdate(guiDrugBrand);
			if(!isValidDrugBrandUpdate) {
				addActionError("Failed to update.This drug brand is available");
			}
			
			if(hasActionErrors()) {
				return ActionResult.ERROR;
			} else {
				guiDrugBrand = drugBrandLogic.updateDrugBrand(guiDrugBrand);  // catch guiDrugBrand object details returned from inserDrugBrand()				
			}	
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String list(){
		if(isDoctorLogin() || isAssistantLogin()) {
			Index index = CommonUtil.getIndexBypage(page);
			if (StringUtils.isNotBlank(term)) {
				guiDrugBrands = drugBrandLogic.getDrugBrandListByTerm(term, index);				
				guiPagination = drugBrandLogic.getPaginationOfDrugBrandsByTerm(term, page);
				size = guiPagination.getTotalCount();
			} else {
				guiDrugBrands = drugBrandLogic.getDrugBrandList(index);
				guiPagination = drugBrandLogic.getPaginationOfDrugBrands(page);
				size = guiPagination.getTotalCount();
			}		
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;		
	}

	public DrugBrandLogic getDrugBrandLogic() {
		return drugBrandLogic;
	}

	public void setDrugBrandLogic(DrugBrandLogic drugBrandLogic) {
		this.drugBrandLogic = drugBrandLogic;
	}

	public GuiDrugBrand getGuiDrugBrand() {
		return guiDrugBrand;
	}

	public void setGuiDrugBrand(GuiDrugBrand guiDrugBrand) {
		this.guiDrugBrand = guiDrugBrand;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public boolean isTermExist() {
		return StringUtils.isNotBlank(term);
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}
		
	public SortProperty getSort() {
		return sort;
	}

	public void setSort(SortProperty sort) {
		this.sort = sort;
	}

	public List<GuiDrugBrand> getGuiDrugBrands() {
		return guiDrugBrands;
	}

	public void setGuiDrugBrands(List<GuiDrugBrand> guiDrugBrands) {
		this.guiDrugBrands = guiDrugBrands;
	}

	public long getSize() {
		return size;
	}

	public void setSize(long size) {
		this.size = size;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public GuiPagination getGuiPagination() {
		return guiPagination;
	}

	public void setGuiPagination(GuiPagination guiPagination) {
		this.guiPagination = guiPagination;
	}
	
}
